<?php

include( "./admin/include/func.php");

if ( ! User::is_logged_in()) header ('Location: index.php');

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$panels = array ();

$panel = array (
	'caption' => 'Új jelszó',
	'title'   => 'Új jelszó',
	'body'    => '',
	'height'  => '120'
);

array_push( $panels, $panel);

if (isset($_POST['modosit'])) {
	
	//print_r ($_POST);
	
    $p_jelszo = trim(strip_tags($_POST['jelszo']));
    $p_uj     = trim(strip_tags($_POST['uj']));

    $success = true;
    $errors = array();
    
    if ($p_jelszo == '') {
        $success = false;
        array_push( $errors, "A Jelenlegi jelszó nincs megadva!");
    } 
	   
    if ($p_uj == '') {
        $success = false;
        array_push( $errors, "Az Új jelszó nincs megadva!");
    } 
   
    if ($p_uj != '' && ! preg_match( '/^[\s\S]{3,10}$/u', $p_uj)) {
        $success = false;
        array_push( $errors, "Az Új jelszó 3-10 karakter lehet!");
    }

    if ($success) {
		$db->sql = "SELECT id FROM users WHERE id='".User::get_id()."' AND jelszo='$p_jelszo'";
		$db->query();
		if (mysql_num_rows( $db->rs) < 1) {
			$success = false;
			array_push( $errors, "A Jelenlegi jelszó nem megfelelő!");
		}
	}
		
    if ( ! $success) {

		$body    = "<ul class='errors'>";
		foreach ($errors as $err) $body .= "<li>$err</li>";
		$body   .= "</ul>";
		
		$error_panel = array (
			'caption' => 'Hiba',
			'title'   => 'Kérjük javítsa ki az alábbi hibákat!',
			'body'    => $body,
			'height'  => count( $errors) * 25 + 10
		);
		
		array_unshift( $panels, $error_panel);
				
	} else {
		
		$db->sql = "UPDATE users SET jelszo='$p_uj' WHERE id='".User::get_id()."'";
		$db->query();
		
		$panels = array ();
		
		$panel = array (
			'caption' => 'Információ',
			'title'   => 'Sikeres jelszó módosítás!',
			'body'    => "<p class='success'>A jelszavát frissítettük!</p>",
			'height'  => '20'
		);

		array_push( $panels, $panel);
	}
}
		    
?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">
	
<?php foreach( $panels as $panel) { ?>
	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption"><?php print $panel['caption']; ?></p>
				<p class="hptitle"><?php print $panel['title']; ?></p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:<?php print $panel['height']; ?>px">
				<?php if ($panel['body'] != '') print $panel['body']; else { ?>
					<form name="form_profilom" id="form_profilom" method="post" enctype='application/x-www-form-urlencoded' accept-charset="utf-8">

						<label for='jelszo' class='required'>Jelenlegi jelszó</label>
						<input name='jelszo' type='password' value="<?php print isset($p_jelszo)?$p_jelszo:''; ?>" />
						<br />

						<label for='uj' class='required'>Új jelszó</label>
						<input name='uj' type='password' value="<?php print isset($p_uj)?$p_uj:''; ?>" />
						<br />
											
						<br />
											
						<label for="modosit">&nbsp;</label>
						<input name="modosit" type="submit" value="Módosít"/>				

					</form>  
				<?php } ?>           			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	<br />

<?php } ?>

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
